﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class user_clerk : MxWeiXinPF.Web.UI.ManagePage
    {
        protected string action = MXEnums.ActionEnum.Add.ToString();
        protected MxWeiXinPF.Model.manager admin_info;
        private int id = 0;
        private int wid = 0;

        protected void bnSelectUser_Click(object sender, EventArgs e)
        {
            base.ChkAdminLevel("user_shop", MXEnums.ActionEnum.View.ToString());
            this.gv_DataBindUser();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                base.ChkAdminLevel("user_shop", MXEnums.ActionEnum.View.ToString());
                if (admin_info == null)
                {
                    base.JscriptMsg("未选择店铺！", "user_list.aspx", "失败");
                    return;
                }
                if (string.IsNullOrEmpty(this.hshopid.Value))
                {
                    base.JscriptMsg("未选择店铺！", "user_list.aspx", "失败");
                    return;
                }

                BLL.manager bm = new BLL.manager();
                Model.manager mm = bm.GetModel(int.Parse(this.hshopid.Value));
                if (mm == null)
                {
                    base.JscriptMsg("选择店铺失效！", "user_list.aspx", "失败");
                    return;
                }

                Model.users muser = new BLL.users().GetModel(int.Parse(this.hid.Value));
                if (muser == null)
                {
                    base.JscriptMsg("选择用户失效！", "user_list.aspx", "失败");
                    return;
                }
                mm.shopclerkid = muser.wxOpenId;
                try
                {
                    if (bm.Update(mm))
                    {
                        base.JscriptMsg("指定成功！", "user_list.aspx", "Success");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    base.JscriptMsg(ex.Message, "", "Error");
                    return;
                }
                base.JscriptMsg("指定失败！", "", "Error");
            }
            catch (Exception ex)
            {
                base.JscriptMsg("指定失败！", "", "Error");
            }

            //string cmdText = "sp_usersshopid";
            
            //SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBString"].ConnectionString);
            //SqlCommand command = new SqlCommand(cmdText, connection)
            //{
            //    CommandType = CommandType.StoredProcedure
            //};
            //command.Parameters.Add("@ID", SqlDbType.Int);
            //command.Parameters["@ID"].Value = int.Parse(this.hid.Value);
            //if (!this.hshopid.Value.Equals(""))
            //{
            //    command.Parameters.Add("@shopid", SqlDbType.Int);
            //    command.Parameters["@shopid"].Value = int.Parse(this.hshopid.Value);
            //}
            //try
            //{
            //    connection.Open();
            //    command.ExecuteNonQuery();
            //    base.JscriptMsg("指定成功！", "user_list.aspx", "Success");
            //}
            //catch (SqlException exception)
            //{
            //    base.JscriptMsg(exception.Errors[0].Message, "user_list.aspx", "Error");
            //}
            //catch (Exception exception2)
            //{
            //    base.JscriptMsg(exception2.Message, "", "Error");
            //}
            //finally
            //{
            //    connection.Close();
            //}
        }

        protected void dgUser_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "SelectName")
            {
                int itemIndex = e.Item.ItemIndex;
                this.tbparentname.Text = this.dgUser.Items[itemIndex].Cells[2].Text;
                this.hshopid.Value = this.dgUser.Items[itemIndex].Cells[1].Text;
                this.DivDoctorList.Style["Display"] = "None";
            }
        }

        private void gv_DataBindUser()
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBString"].ConnectionString))
            {
                connection.Open();
                string selectCommandText = "[sp_FindManager2]";
                try
                {
                    SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, connection)
                    {
                        SelectCommand = { CommandType = CommandType.StoredProcedure }
                    };
                    if (!this.tbparentname.Text.Equals(""))
                    {
                        adapter.SelectCommand.Parameters.Add(new SqlParameter("@nickname", SqlDbType.VarChar, 50));
                        adapter.SelectCommand.Parameters["@nickname"].Value = this.tbparentname.Text.Trim();
                    }
                    //if (this.admin_info != null)
                    //{
                    //    adapter.SelectCommand.Parameters.Add(new SqlParameter("@wid", SqlDbType.Int));
                    //    adapter.SelectCommand.Parameters["@wid"].Value = this.admin_info.id;
                    //}
                    //adapter.SelectCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
                    //adapter.SelectCommand.Parameters["@id"].Value = this.id;
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "Table");
                    this.dgUser.DataSource = dataSet;
                    this.dgUser.DataBind();
                    if (this.dgUser.Items.Count == 1)
                    {
                        this.tbparentname.Text = this.dgUser.Items[0].Cells[2].Text;
                        this.hshopid.Value = this.dgUser.Items[0].Cells[1].Text;
                        this.DivDoctorList.Style["Display"] = "None";
                    }
                    else if (this.dgUser.Items.Count > 1)
                    {
                        this.DivDoctorList.Style["Display"] = "Block";
                    }
                    else
                    {
                        base.JscriptMsg("找不到跟输入内容相关的内容，请重新输入！", "", "Success");
                    }
                }
                catch (SqlException exception)
                {
                    base.JscriptMsg(exception.Errors[0].Message, "", "Error");
                }
                catch (Exception exception2)
                {
                    base.JscriptMsg(exception2.Message, "", "Error");
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            this.DivDoctorList.Style["Display"] = "None";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.admin_info = base.GetAdminInfo();
            string queryString = MXRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(queryString) && (queryString == MXEnums.ActionEnum.Edit.ToString()))
            {
                this.action = MXEnums.ActionEnum.Edit.ToString();
                this.id = MXRequest.GetQueryInt("id");
                this.wid = MXRequest.GetQueryInt("wid");
                if (this.id == 0)
                {
                    base.JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!new MxWeiXinPF.BLL.users().Exists(this.id))
                {
                    base.JscriptMsg("信息不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!this.Page.IsPostBack)
            {
                base.ChkAdminLevel("user_parent", MXEnums.ActionEnum.View.ToString());
                this.admin_info = base.GetAdminInfo();
                if (this.action == MXEnums.ActionEnum.Edit.ToString())
                {
                    this.ShowInfo(this.id);
                }
            }
        }

        private void ShowInfo(int _id)
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBString"].ConnectionString);
            SqlCommand command = new SqlCommand("select a.*,c.real_name as shopname from dt_users a left join dt_manager c on a.shopid=c.id where a.ID=" + _id, connection);
            connection.Open();
            try
            {
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    //this.tbparentname.Text = reader["shopname"].ToString();
                    this.hshopid.Value = reader["shopid"].ToString();
                    this.txtNickName.Text = reader["nick_name"].ToString();
                    this.img.ImageUrl = reader["avatar"].ToString();
                    this.hid.Value = reader["id"].ToString();
                }
                reader.Close();
            }
            catch (SqlException exception)
            {
                base.JscriptMsg(exception.Errors[0].Message, "", "Error");
            }
            catch (Exception exception2)
            {
                base.JscriptMsg(exception2.Message, "", "Error");
            }
            finally
            {
                connection.Close();
            }
        }
    }
}